ShowTable of Contents
Here is a full sample program for writing a Java interface to Notes/Domino classes. (This posting builds on a previous one http://www-10.lotus.com/ldd/ddwiki.nsf/dx/08052009082228AMWEBGJ4.htm.)
The full source code:
http://www.chc-3.com/downloads/NotesJavaShell.zip
Other Notes/Domino downloads:
http://www.chc-3.com/downloads.php
Chuck Connell
++++++++++++++++++
/*
* Chuck Connell, www.chc-3.com, June 2010.
* Example of general shell for Java programming for IBM/Lotus Notes and Domino.
*
* The core idea is from Bob Balaban(www.bobzblog.com, bbalaban@gmail.com).
* I added the configuration database, logging, and more comments.
*/
import lotus.domino.*;
public class NotesJavaShell extends AgentBase{
Public fields.
Local vars
private Session session;
private Database database;
Constructors
public NotesJavaShell(){ default constructor, without run context
}
public NotesJavaShell(Session s, Database d) { when we know the run context
this.session = s;
this.database = d;
}
Start here when run from Eclipse.
public static void main(String[] args) {
Session sess=null;
Database db=null;
NotesJavaShell ag;
Initialize Notes
NotesThread.sinitThread();
try {
sess = NotesFactory.createSession(); start a Notes session
db = sess.getDatabase("", Constants.DEFAULT_HOME_DB); simulate home database for agent
ag = new NotesJavaShell(sess, db); simulate agent object with session and database
ag.NotesMain(); call main routine of agent
}
catch(NotesException ne) {
System.out.println(ne.id + " " + ne.text);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (db != null) db.recycle();
if (sess != null) sess.recycle();
}
catch (Exception x) {}
NotesThread.stermThread();
}
} end main
This routine is called however the code is run: from Eclipse or agent launch within Notes/Domino.
public void NotesMain()
{
Log notesLog=null;
Session sess=null;
Database db=null;
AgentContext ac=null;
Utilities util;
int logLevel;
try {
if (this.session != null) Already have an agent context.
{
sess = this.session;
db = this.database;
}
else { Need to get agent context.
sess = this.getSession();
if (sessnull) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not get current session at start of agent.");
ac = sess.getAgentContext();
if (acnull) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not get agent context at start of agent.");
db = ac.getCurrentDatabase();
if (db==null) throw new NotesException(NotesError.NOTES_ERR_ERROR, "Could not get current database at start of agent.");
}
}
catch(NotesException ne) {
System.out.println(ne.id + " " + ne.text);
return;
}
catch (Exception e) {
e.printStackTrace();
return;
}
finally {};
The truly common code, however we are invoked.
try {
Get utilities.
util = new Utilities();
Record the session so other objects can use it.
util.setSession(sess);
Find all the runtime parameters.
util.readParameters();
Start a Notes log.
notesLog = util.getNotesLog();
logLevel = util.getLogLevel();
if (Constants.CONSOLE_OUTPUT) System.out.println("Agent started. " + Constants.BUILD_STAMP);
if (logLevel >= Constants.LOG_LEVEL_BRIEF) notesLog.logAction ("Agent started. " + Constants.BUILD_STAMP);
******* Do the real work here ********
System.out.println (db.getTitle()); print title of home database, to prove we got the agent context correctly
******* Do the real work here ********
All done with no errors.
if (logLevel >= Constants.LOG_LEVEL_BRIEF) notesLog.logAction ("Agent done with no errors.");
if (Constants.CONSOLE_OUTPUT) System.out.println("Agent done with no errors.");
} try
Catch Notes exceptions from main code.
catch(NotesException ne) {
if (Constants.CONSOLE_OUTPUT) System.out.println("Notes error code " + ne.id + ". " + ne.text);
try {
notesLog.logError (ne.id, ne.text); Notes log may not be valid, but give it a try
}
catch (Exception x) {}
?? }
Catch non-Notes exceptions from main code.
catch (Exception x) {
if (Constants.CONSOLE_OUTPUT) System.out.println("Error: " + x.getMessage());
if (Constants.CONSOLE_OUTPUT) x.printStackTrace();
try {
notesLog.logError (NotesError.NOTES_ERR_ERROR, x.getMessage()); Notes log may not be valid, but give it a try
}
catch (Exception x2) {}
}
Do cleanup from main code block.
finally {
try {
if (notesLog!=null) notesLog.close();
if (notesLog!=null) notesLog.recycle();
cannot recycle the AgentContext, Session or Database, since they may have been passed in by Notes.
}
catch (Exception x){}
} finally
} NotesMain
} main class